package com.moreemrecife.ui.beans;

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.InitialContext;

import com.moreemrecife.dto.FunctionTO;
import com.moreemrecife.security.AccessControl;
import com.moreemrecife.ui.MessageHelper;

/**
 * Action Bean for Function screen 
 */
@ManagedBean(name = "functionBean")
@RequestScoped
public class FunctionActionBean implements Serializable {

    Logger log = Logger.getLogger(FunctionActionBean.class.getName());

    private static final long serialVersionUID = -6504175133243857258L;

    private int id;
    private String name;
    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String saveFunction() {
        FunctionTO to = new FunctionTO();

        to.setId(getId());
        to.setName(getName());
        to.setDescription(getDescription());

        try {
            InitialContext ctx = new InitialContext();
            Object o = ctx.lookup("java:global/RealEstateEAR/RealEstateSecurityEJB/AccessControlBean!com.moreemrecife.security.AccessControl");

            AccessControl ac = (AccessControl)o;// javax.rmi.PortableRemoteObject.narrow(o, AccessControl.class);
            int id = ac.saveFunction(to);
//            int id = ac.testCall();

            // Boring result message
            MessageHelper.saveSuccess(id);

        } catch (Exception e) {
            log.log(Level.SEVERE, "Error updating Function", e);

            MessageHelper.reportError("fnCrudErrorSaving");
            return "fail";
        }
        return "success";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
